/**
 * Created by wangmeng on 2018/8/6.
 */
var param = new Object();
var url = path + "/common/exportExcel/listTableColumnByName";
$(function () {
    var cellgrid = $("#celltt");
    initCellGrid(cellgrid);
    cellgrid.datagrid({
        fit:false
    });
    cellgrid.datagrid("resize",{
        height:450
    });
});

//查询按钮
function btnQuery() {
    param.tableName = $.trim($("#tableName").val()).toUpperCase();
    var grid = $("#celltt");
    getFieldData(grid, param, url)
}

//重置按钮
function btnReset() {
    $("#tableName").val('');
    var grid = $("#celltt");
    grid.datagrid('loadData', {total : 0, rows : []});
}

//初始化单元格定义表格和数据
function initCellGrid(grid) {
    var columns = [
        {
            field: "columnName",
            title: "取数字段",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 200
        }
    ];
    initGridWithCheckBoxCanEdit(grid, param, url, columns, "#celltb", "id");
    var options = $("#celltt").datagrid("options");
    options.onLoadSuccess = function (data) {
        $('#celltt').datagrid("fixRownumber");
    };
    grid.datagrid({
        title:'字段信息'
    })
}

//初始化 DataGrid控件信息带checkbox多选不分页 (默认选中)
function initGridWithCheckBoxCanEdit(grid, param, url, columns, toolbar, fieldId) {
    grid.datagrid({
        url : "",
        title : "",
        toolbar : toolbar,
        fit:false,
        loadMsg : "",
        queryParams : {},
        nowrap : true,
        striped : true,
        idField : fieldId,
        remoteSort : false,
        frozenColumns : [ [ {
            field : 'check',
            checkbox : true
        } ] ],
        columns : [ columns ],
        onLoadSuccess : function(data) {
            grid.datagrid('clearSelections');
        },
        onLoadError : function() {
            $("#loading").hide();
        }
    });
}

//grid不分页查询
function getFieldData(grid, param, url) {
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: param,
        success: function callback(data) {
            var analyMsg = data;
            if (analyMsg.code != '0000') {
                $.messager.alert("操作提示", analyMsg.msg, "error", function () {
                });
            } else {
                if (analyMsg.returnObject.total > 0) {
                    grid.datagrid("loadData", analyMsg.returnObject);
                } else {
                    grid.datagrid('loadData', {total : 0, rows : []
                    });
                }
            }
        },
        error: function (data, textstatus) {
        }
    });
}

//保存增加的数据
function submit_Add() {
    var rows = $('#celltt').datagrid('getSelections');
    var columnNames = "";
    for (var i=0; i<rows.length; i++) {
        var item = rows[i];
        var columnName = item.columnName;
        if(columnNames == ""){
            columnNames = columnName;
        }else {
            columnNames += ","+columnName;
        }
    }
    setGlobalVariable("columnNames",columnNames);
    closeLayer();
}